package com.akk.platform.manage;

import com.akk.module.admin.entity.Admin;
import com.akk.module.admin.entity.AdminMenu;
import com.akk.module.admin.entity.AdminRole;
import com.akk.platform.manage.business.AdminBusiness;
import com.akk.platform.manage.business.AdminMenuBusiness;
import com.akk.platform.manage.business.AdminRoleBusiness;
import com.akk.task.ScheduledTasks;
import com.akk.utils.Constants;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

@RestController
@CrossOrigin
@RequestMapping("/platform/manage")
@Api(value = "manage-admin",description = "平台后台接口-ADMIN")
public class AdminAction {

    @Autowired
    AdminBusiness adminBusiness;
    @Autowired
    AdminMenuBusiness adminMenuBusiness;
    @Autowired
    AdminRoleBusiness adminRoleBusiness;

    @Autowired
    private ScheduledTasks scheduledTasks;

    @GetMapping(value = "/send")
    public RestResponse send(){
        scheduledTasks.sendRecordMail();
        return RestResponse.build("success");
    }


    @GetMapping(value = "/adminlist")
    @RequiresPermissions("manage:admin:list")
    @ApiOperation(value = "管理员列表",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET", response = RestResponse.class)
    public RestResponse adminList(@Valid Query query){
        return adminBusiness.adminList(query,ShiroUtils.getAdmin());
    }

    @PostMapping(value = "/adminadd")
    @RequiresPermissions("manage:admin:add")
    @ApiOperation(value = "管理员添加",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "POST", response = RestResponse.class)
    public RestResponse adminAdd(@RequestBody @Valid Admin admin){
        return adminBusiness.adminAdd(admin,ShiroUtils.getAdmin());
    }

    @PostMapping("/adminupdate")
    @RequiresPermissions("manage:admin:update")
    @ApiOperation(value = "管理员修改",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "POST", response = RestResponse.class)
    public RestResponse adminUpdate(@RequestBody @Valid Admin admin){
        return adminBusiness.adminUpdate(admin);
    }

    @RequestMapping("/admininfo/{adminId}")
    @RequiresPermissions("manage:admin:info")
    @ApiOperation(value="管理员信息",httpMethod = "GET",response = RestResponse.class)
    public RestResponse adminInfo(@PathVariable String adminId){
        return adminBusiness.adminInfo(adminId);
    }

    @GetMapping("/admindel/{adminId}")
    @RequiresPermissions("manage:admin:delete")
    @ApiOperation(value = "管理员删除", httpMethod = "GET", response = RestResponse.class)
    public RestResponse adminDel(@PathVariable String adminId){
        return adminBusiness.adminDel(adminId, ShiroUtils.getAdmin());
    }

    @RequestMapping("/adminrole")
    @RequiresPermissions("manage:admin:role")
    @ApiOperation(value = "管理员角色",notes = "管理员角色",httpMethod = "GET",response = RestResponse.class)
    public RestResponse adminRole(String appId){
        return adminRoleBusiness.adminRole(appId,ShiroUtils.getAdmin());
    }


    @GetMapping("/menulist")
    @RequiresPermissions("manage:menu:list")
    @ApiOperation(value = "菜单列表",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET", response = RestResponse.class)
    public RestResponse menuList(@Valid Query query){
        return adminMenuBusiness.menuList(query);
    }

    @GetMapping("/allmenutree")
    @RequiresPermissions("manage:menu:tree")
    @ApiOperation(value = "菜单树",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET", response = RestResponse.class)
    public RestResponse allMenuTree(){
        return adminMenuBusiness.allMenuTree();
    }

    @GetMapping("/rolemenutree")
    @RequiresPermissions("manage:role:menu:tree")
    @ApiOperation(value = "角色菜单树",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET", response = RestResponse.class)
    public RestResponse roleMenuTree(){
        Admin admin = ShiroUtils.getAdmin();
        if(admin.getAdminId().equals(Constants.PLATFORM_MANAGE_ADMIN_ID)){
            return adminMenuBusiness.allMenuTree();
        }
        return adminMenuBusiness.roleMenuTree(admin.getRoleId());
    }


    @PostMapping("/menuupdate")
    @RequiresPermissions("manage:menu:update")
    @ApiOperation(value = "菜单修改",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "POST", response = RestResponse.class)
    public RestResponse menuUpdate(@RequestBody AdminMenu adminMenu){
        return adminMenuBusiness.menuUpdate(adminMenu);
    }

    @PostMapping("/menuadd")
    @RequiresPermissions("manage:menu:add")
    @ApiOperation(value = "菜单添加",produces = MediaType.APPLICATION_JSON_VALUE,httpMethod = "POST",response = RestResponse.class)
    public RestResponse menuAdd(@RequestBody AdminMenu adminMenu) {
        return adminMenuBusiness.menuAdd(adminMenu);
    }

    @GetMapping("/menudel/{menuId}")
    @RequiresPermissions("manage:menu:delete")
    @ApiOperation(value = "菜单删除", httpMethod = "GET", response = RestResponse.class)
    public RestResponse menuDel(@PathVariable String menuId){
        return adminMenuBusiness.menuDel(menuId);
    }

    @RequestMapping("/menuinfo/{menuId}")
    @RequiresPermissions("manage:menu:info")
    @ApiOperation(value = "菜单详情", httpMethod = "GET", response = RestResponse.class)
    public RestResponse menuInfo(@PathVariable String menuId) {
        return adminMenuBusiness.menuInfo(menuId);
    }


    @GetMapping("/rolelist")
    @RequiresPermissions("manage:role:list")
    @ApiOperation(value = "角色列表",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "GET", response = RestResponse.class)
    public RestResponse roleList(@Valid Query query) {
        return adminRoleBusiness.roleList(query,ShiroUtils.getAdmin());
    }

    @PostMapping("/roleadd")
    @RequiresPermissions("manage:role:add")
    @ApiOperation(value = "角色添加",produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "POST", response = RestResponse.class)
    public RestResponse roleAdd(@RequestBody AdminRole adminRole){
        return adminRoleBusiness.roleAdd(adminRole);
    }

    @RequestMapping("/roledel/{roleId}")
    @RequiresPermissions("manage:role:delete")
    @ApiOperation(value = "角色删除",httpMethod = "GET",response = RestResponse.class)
    public RestResponse roleDel(@PathVariable String roleId){
        return adminRoleBusiness.roleDel(roleId,ShiroUtils.getAdmin());
    }

    @RequestMapping(value = "/roleinfo/{roleId}")
    @RequiresPermissions("manage:role:info")
    @ApiOperation(value = "角色信息",httpMethod = "GET",response = RestResponse.class)
    public RestResponse roleInfo(@PathVariable String roleId) {
        return adminRoleBusiness.roleInfo(roleId);
    }

    @RequestMapping("/roleupdate")
    @RequiresPermissions("manage:role:update")
    @ApiOperation(value = "角色修改",httpMethod = "GET",response = RestResponse.class)
    public RestResponse roleUpdate(@RequestBody AdminRole adminRole){
        return adminRoleBusiness.roleUpdate(adminRole);
    }

    @RequestMapping(value = "/rolemenus/{roleId}")
    @RequiresPermissions("manage:role:menu")
    @ApiOperation(value = "角色菜单",httpMethod = "GET",response = RestResponse.class)
    public RestResponse roleMenus(@PathVariable String roleId){
        return adminRoleBusiness.roleMenus(roleId);
    }

}
